package sharm.web;

import sharm.pojo.Book;
import sharm.pojo.Comment;
import sharm.pojo.Page;
import sharm.service.BookService;
import sharm.service.CommentService;
import sharm.service.impl.BookServiceImpl;
import sharm.service.impl.CommentServiceImpl;
import sharm.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author ViTe
 * @create 2022/6/4 - 18:55
 */
@WebServlet(name = "CommentServlet", value = "/CommentServlet")
public class CommentServlet extends BaseServlet{

    private CommentService commentService = new CommentServiceImpl();

    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1 获取请求的参数 pageNo 和 pageSize
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        String gamename = req.getParameter("gamename");
        if (gamename == null) gamename = "绝地求生";
        //2 调用BookService.page(pageNo，pageSize)：Page对象
        Page<Comment> page = commentService.page(pageNo,pageSize, gamename);
//        for (Comment c: page.getItems()
//             ) {
//            System.out.println(c.getName());
//        }
        page.setUrl("CommentServlet?action=page");
        //3 保存Page对象到Request域中
        req.setAttribute("page",page);
        //4 请求转发到pages/manager/book_manager.jsp页面
        req.getRequestDispatcher("/pages/games/pubg.jsp").forward(req,resp);
    }
    protected void addComment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1 获取请求的参数 content
        String content = req.getParameter("content");
        Integer id = 11;
        String gamename = "绝地求生";

        Comment comment = new Comment();
        comment.setContent(content);
        comment.setId(id);
        comment.setGamename(gamename);

        commentService.addComment(comment);

        req.getRequestDispatcher("CommentServlet?action=page").forward(req,resp);
    }


}
